package com.geektime.hello;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;

/**
 * @Author: DRevelation
 * @Date: 2021/1/18 15:21
 */
@Slf4j
public class JsonDemo {
    public static void main(String[] args) {
        String str = "[\n" +
                "    {\n" +
                "        \"clarity\": \"超清\",\n" +
                "        \"url\": \"http://dbxz1-hw.ahtv.cn/ahtv-transcoding/20201113/87d78b9d-9c8a-b89d-f766-248471a52f83_H.265_1920x1080_AAC_2186.mp4\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"clarity\": \"高清\",\n" +
                "        \"url\": \"http://dbxz1-hw.ahtv.cn/ahtv-transcoding/20201113/87d78b9d-9c8a-b89d-f766-248471a52f83_H.265_1280x720_AAC_1400.mp4\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"clarity\": \"标清\",\n" +
                "        \"url\": \"http://dbxz1-hw.ahtv.cn/ahtv-transcoding/20201113/87d78b9d-9c8a-b89d-f766-248471a52f83_H.265_854x480_AAC_500.mp4?t=1605260431&k=125fd2f5b806ed339f7842c37d40844a\"\n" +
                "    }\n" +
                "]";
        JSONArray videoArray = JSON.parseArray(str);
        String url = videoArray.getJSONObject(0).getString("url");
        System.out.println("url = " + url);


        String str1 = "{\n" +
                "    \"total\": 1,\n" +
                "    \"is_truncated\": 0,\n" +
                "    \"task_array\": [\n" +
                "        {\n" +
                "            \"output_file_name\": [\n" +
                "                \"c8065691-52cb-1e64-2696-bd58debdf1d0_H.265_1920x1080_AAC_1597.mp4\",\n" +
                "                \"c8065691-52cb-1e64-2696-bd58debdf1d0_H.265_1280x720_AAC_1400.mp4\",\n" +
                "                \"c8065691-52cb-1e64-2696-bd58debdf1d0_H.265_854x480_AAC_500.mp4\"\n" +
                "            ],\n" +
                "            \"create_time\": \"20210127124525\",\n" +
                "            \"end_time\": \"20210127130505\",\n" +
                "            \"trans_template_id\": [\n" +
                "                281349,\n" +
                "                281350,\n" +
                "                281351\n" +
                "            ],\n" +
                "            \"task_id\": \"42128497\",\n" +
                "            \"pic_info\": [],\n" +
                "            \"output\": {\n" +
                "                \"bucket\": \"ahtv-obs\",\n" +
                "                \"location\": \"cn-north-4\",\n" +
                "                \"object\": \"ahtv-transcoding/20210127\"\n" +
                "            },\n" +
                "            \"input\": {\n" +
                "                \"bucket\": \"ahtv-obs\",\n" +
                "                \"location\": \"cn-north-4\",\n" +
                "                \"object\": \"ahtv-obs/20210127/c8065691-52cb-1e64-2696-bd58debdf1d0.mp4\"\n" +
                "            },\n" +
                "            \"transcode_detail\": {\n" +
                "                \"input_file\": {\n" +
                "                    \"duration\": 226,\n" +
                "                    \"duration_ms\": 225237,\n" +
                "                    \"audio_info\": [\n" +
                "                        {\n" +
                "                            \"codec\": \"AAC\",\n" +
                "                            \"channels\": 2,\n" +
                "                            \"bitrate\": 576,\n" +
                "                            \"sample\": 48000,\n" +
                "                            \"bitrate_bps\": 576000\n" +
                "                        }\n" +
                "                    ],\n" +
                "                    \"size\": 61426528,\n" +
                "                    \"format\": \"MOV,MP4,M4A,3GP,3G2,MJ2\",\n" +
                "                    \"video_info\": {\n" +
                "                        \"codec\": \"H264\",\n" +
                "                        \"width\": 1920,\n" +
                "                        \"bitrate\": 1598,\n" +
                "                        \"bitrate_bps\": 1598000,\n" +
                "                        \"frame_rate\": 30,\n" +
                "                        \"height\": 1080\n" +
                "                    }\n" +
                "                },\n" +
                "                \"multitask_info\": [\n" +
                "                    {\n" +
                "                        \"template_id\": 281349,\n" +
                "                        \"output_file\": {\n" +
                "                            \"duration\": 226,\n" +
                "                            \"duration_ms\": 225237,\n" +
                "                            \"audio_info\": [\n" +
                "                                {\n" +
                "                                    \"codec\": \"AAC\",\n" +
                "                                    \"channels\": 2,\n" +
                "                                    \"bitrate\": 576,\n" +
                "                                    \"sample\": 48000,\n" +
                "                                    \"bitrate_bps\": 576000\n" +
                "                                }\n" +
                "                            ],\n" +
                "                            \"size\": 40023,\n" +
                "                            \"format\": \"MP4\",\n" +
                "                            \"video_info\": {\n" +
                "                                \"codec\": \"H265\",\n" +
                "                                \"width\": 1920,\n" +
                "                                \"bitrate\": 845,\n" +
                "                                \"bitrate_bps\": 845964,\n" +
                "                                \"frame_rate\": 30,\n" +
                "                                \"height\": 1080\n" +
                "                            }\n" +
                "                        }\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"template_id\": 281350,\n" +
                "                        \"output_file\": {\n" +
                "                            \"duration\": 226,\n" +
                "                            \"duration_ms\": 225237,\n" +
                "                            \"audio_info\": [\n" +
                "                                {\n" +
                "                                    \"codec\": \"AAC\",\n" +
                "                                    \"channels\": 2,\n" +
                "                                    \"bitrate\": 576,\n" +
                "                                    \"sample\": 48000,\n" +
                "                                    \"bitrate_bps\": 576000\n" +
                "                                }\n" +
                "                            ],\n" +
                "                            \"size\": 37998,\n" +
                "                            \"format\": \"MP4\",\n" +
                "                            \"video_info\": {\n" +
                "                                \"codec\": \"H265\",\n" +
                "                                \"width\": 1280,\n" +
                "                                \"bitrate\": 773,\n" +
                "                                \"bitrate_bps\": 773996,\n" +
                "                                \"frame_rate\": 30,\n" +
                "                                \"height\": 720\n" +
                "                            }\n" +
                "                        }\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"template_id\": 281351,\n" +
                "                        \"output_file\": {\n" +
                "                            \"duration\": 226,\n" +
                "                            \"duration_ms\": 225237,\n" +
                "                            \"audio_info\": [\n" +
                "                                {\n" +
                "                                    \"codec\": \"AAC\",\n" +
                "                                    \"channels\": 2,\n" +
                "                                    \"bitrate\": 576,\n" +
                "                                    \"sample\": 48000,\n" +
                "                                    \"bitrate_bps\": 576000\n" +
                "                                }\n" +
                "                            ],\n" +
                "                            \"size\": 26035,\n" +
                "                            \"format\": \"MP4\",\n" +
                "                            \"video_info\": {\n" +
                "                                \"codec\": \"H265\",\n" +
                "                                \"width\": 854,\n" +
                "                                \"bitrate\": 348,\n" +
                "                                \"bitrate_bps\": 348906,\n" +
                "                                \"frame_rate\": 30,\n" +
                "                                \"height\": 480\n" +
                "                            }\n" +
                "                        }\n" +
                "                    }\n" +
                "                ]\n" +
                "            },\n" +
                "            \"status\": \"SUCCEEDED\"\n" +
                "        }\n" +
                "    ]\n" +
                "}";


        JSONObject jsonObject = JSONObject.parseObject(str1);

        List<Map<String, Object>> listForTranscoding = (List<Map<String, Object>>) jsonObject.get("task_array");
        for (Map<String, Object> stringObjectMap : listForTranscoding) {
            JSONArray outputFileName = JSON.parseArray(stringObjectMap.get("output_file_name").toString());
            for (int i = 0; i < outputFileName.size(); i++) {
                String matTranscodeUrl = outputFileName.getString(i);

                JSONObject transcodeDetail = JSON.parseObject(stringObjectMap.get("transcode_detail").toString());
                JSONArray multitaskInfos = transcodeDetail.getJSONArray("multitask_info");
                for (int j = 0; j < multitaskInfos.size(); j++) {
                    JSONObject multitaskInfo = multitaskInfos.getJSONObject(j);
                    if (multitaskInfo != null) {
                        JSONObject outputFile = multitaskInfo.getJSONObject("output_file");
                        JSONObject videoInfo = outputFile.getJSONObject("video_info");
                        String videoWidth = videoInfo.getString("width");
                        String videoHeight = videoInfo.getString("height");
                        if (matTranscodeUrl.contains(videoWidth) && matTranscodeUrl.contains(videoHeight)) {
                            double size = outputFile.getDouble("size") / 1024;
                            double duration = outputFile.getDouble("duration");
                            System.out.println("size = " + size);
                            System.out.println("duration = " + duration);
                        }
                    }
                }
            }
        }
    }
}
